package com.centit.util;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.centit.fileserver.client.DefaultFileClient;
import com.centit.fileserver.client.po.FileStoreInfo;
import com.centit.framework.appclient.AppSession;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.model.adapter.PlatformEnvironment;

/**
* @ClassName: CommFileUploadController
* @Description: 文件上传工具类
* @author zhangyi
* @date 2018年6月19日
*
*/
@Controller
@MultipartConfig
@RequestMapping("/commom/uploadFile")
public class CommFileUploadController extends BaseController
{
    private static final Log log = LogFactory.getLog(CommFileUploadController.class);
    
    /**
    * @Title: uploadImg
    * @Description: 图片上传方法
    * @param @param file
    * @param @return    参数
    * @return String    返回类型
    * @author zhangyi
    * @date 2018年6月19日
    * @throws
    */
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public JSONObject uploadImg(HttpServletRequest request, @RequestParam("file") MultipartFile file,HttpServletResponse response) {
        if (null != file) {
            String myFileName = file.getOriginalFilename();// 文件原名称
            String tempPath=request.getSession().getServletContext().getRealPath("")+File.separator+"codeTemp"+File.separator;

            File fileDir=new File(tempPath);
            if (!fileDir.exists()) { //如果不存在 则创建     
                 fileDir.mkdirs();    
             } 
            String path=tempPath+myFileName;
            File localFile = new File(path);  
            try {
                //本地生成文件
                file.transferTo(localFile);
                
                //将文件上传到附件服务器
                String tempId="";
                FileStoreInfo fileStoreInfo = new FileStoreInfo();
                
                String fileType = myFileName.substring(myFileName.lastIndexOf(".") + 1);
                // 设置文件名称
                fileStoreInfo.setFileName(myFileName);
                // 设置文件类型
                fileStoreInfo.setFileType(fileType);
                // 获取fileClient值
                DefaultFileClient fileClient = null;
                AppSession appSession = null;
                
              // 配置文件文件服务器路径
                appSession = new AppSession(CodeRepositoryUtil.getSysConfigValue("uploaderpath"));
                fileClient = new DefaultFileClient();
                fileClient.setAppSession(appSession);
                fileClient.setFileServerExportUrl(CodeRepositoryUtil.getSysConfigValue("uploaderpath"));
               
                // 上传文件服务器
                fileStoreInfo = fileClient.uploadFile(fileClient.getHttpClient(),fileStoreInfo,localFile);
                tempId=fileStoreInfo.getFileId();
                
                //程序结束时，删除临时文件
                deleteFile(localFile);
                JSONObject resObj = new JSONObject();
                resObj.put("code", "0");
                resObj.put("fileId", tempId);
                return resObj;
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }else{
            System.out.println("文件为空");
        }
        return null;
    }
    
    /**  
     * 删除  
     *   
     * @param files  
     */  
    private void deleteFile(File... files) {  
        for (File file : files) {  
            if (file.exists()) {  
                file.delete();  
            }  
        }  
    }

}
